package cn.south.campus.admin.campus.service.impl;

import cn.south.campus.admin.campus.mapper.GoodsMapper;
import cn.south.campus.admin.campus.model.entity.Goods;
import cn.south.campus.admin.campus.model.enums.GoodsStatusEnums;
import cn.south.campus.admin.campus.model.req.GoodsReq;
import cn.south.campus.admin.campus.model.vo.GoodsVo;
import cn.south.campus.admin.campus.service.GoodsService;
import cn.south.campus.common.model.enums.CommonEnums;
import cn.south.campus.common.utils.AssertUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 *
 * </p>
 *
 * @author chow
 * @since 2024/11/14 8:59
 */
@Service
@AllArgsConstructor
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    private final GoodsMapper goodsMapper;

    @Override
    public IPage<GoodsVo> pageQuery(GoodsReq req) {
        IPage<Goods> goodsIPage = goodsMapper.pageQuery(req.getPageDto(), req);
        return goodsIPage.convert(GoodsVo::convert);
    }

    @Override
    public GoodsVo detail(Long id) {
        Goods goods = goodsMapper.getById(id);
        return GoodsVo.convert(goods);
    }

    @Override
    public void changeStatus(GoodsStatusEnums goodsStatusEnums, Long id) {
        Goods goods = goodsMapper.getById(id);
        AssertUtils.isNotNull(goods, CommonEnums.DATA_HAS_NOT_ERROR);
        goods.setStatus(goodsStatusEnums.getCode());
        goodsMapper.updateById(goods);
    }
}
